
//
var swiper = new Swiper('.banner', {
	loop : true,
	//speed:300,
	//freeMode : true,
    autoplay : 5000,
	autoHeight: true,
    pagination: '.swiper-pagination',
})
var swiper = new Swiper('.inbannertwo', {
	effect: 'coverflow',
	grabCursor: true,
  	centeredSlides: true,
	slidesPerView: 'auto',
	coverflowEffect: {
	rotate: 2,
	stretch: 0,
	depth: 200,
	modifier: 1,
	slideShadows : false,
	},
    pagination: '.swiper-pagination',
})
var swiper = new Swiper('.indexmenu', {
    // pagination: '.swiper-pagination',
	slidesPerView: 5
})
var swiper = new Swiper('.shopbanner', {
    pagination: '.swiper-pagination',
    paginationClickable: true
})
var swiper = new Swiper('.shopinxban', {
	loop : true,
	speed:300,
    autoplay : 2000,
	direction : 'vertical',
})
var swiper = new Swiper('.duosaixuan', {
	slidesPerView: 'auto',
})

$(function () {
	var weiz = $(".saixuan a.actived").index();
	var wid = $(".saixuan a").width();
	// console.log(wid);
	//document.getOverrideStyle(".saixuan", ':after').display = 'none';
	// console.log($(".saixuan").offsetLeft);
	$(".saixuan .after").animate({left:weiz*wid});
});
$(function(){
	
	$('.shopshow .saixuanfix a').click(function(){
		$(this).addClass("actived").siblings().removeClass("actived");
		var weiz = $(this).index();
		var wid = $(".saixuanfix a").width();
		//document.getOverrideStyle(".saixuan", ':after').display = 'none';
		$(".saixuanfix .after").animate({left:weiz*wid});
		var index = $(this).index();
		$(".shopshowcons .swiper-slide-item").eq(index).show().siblings().hide();
	});
	$(".tabcard a").click(function () {
		$(this).addClass("actived").siblings().removeClass("actived");
		var weiz = $(this).index();
		$(".tabcontent").eq(weiz).addClass("actived").siblings().removeClass("actived");
	})
	$(".itemitembtn").click(function(){

        $(this).siblings(".itemitembox-cons").slideToggle();
	})


	


	function addmoel(xian,thishe,car){
			var s=0;
			$(".item-right").each(function () {
				//console.log(parseInt($(this).find(".zi").text()));
				//console.info($(this).find('.xian').text().replace(/[^0-9]/ig,""));
				if($(this).find(".zi").text()==""){
					//console.log("当前是空");
				}else {
					s+=parseInt($(this).find(".zi").text())*parseFloat($(this).find('.xian').text().replace(/[^0-9]/ig,""));
				}
			})
			$(".shopcarbox .left .mbmin").html('<span class="mr5">共</span>¥'+s.toFixed(2)+'');
			$(".shopcar ul").prepend(car);
	}

	$(".close").on("click",function(){
		$(".guigemero").animate({top:'100%'});
	})
	$(".hotserchbtn").click(function(){
		var c = $(this).attr('class');
		if (c.indexOf('active') == -1) {
            $(this).addClass("active").siblings().removeClass("active");
            if (typeof setExpenseType == 'function') {
                setExpenseType($(this).attr('data-type'));
            }
        }
	})
	//购物车清空
	// $(".removecar").click(function(){
	// 	let html = `
	// 		<li class="heng">
	// 			<div class="">
	// 				<p class="lh10">餐盒费</p>
	// 			</div>
	// 			<div class="heng vCenter">
	// 				<span class="szzz mr10">¥ 60</span>
	// 			</div>
	// 		</li>`
	// 	$(".shopcar ul").html(html);
	// })
	//日期选择器
	$('.showDatePicker').on('click', function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onChange: function (result) {
                console.log(result);
            },
            onConfirm: function (result) {
                console.log(result);
            }
        });
	});

	//配送费明细
	$('.showPeisPicker').on('click', function () {
		$(".mask").fadeIn(500);
		// $('.PeisPicker').addClass('animated fadeInUp');
		// setTimeout(function(){
		// 	$('.PeisPicker').removeClass('fadeInUp');
		// }, 1000);
		$(".FeiXibox").animate({
			bottom: '0', opacity: '1',zIndex:'99999'
		}, 300);
		$(".FeiXibox").fadeIn(500);
	});


	
	//订单选项卡切换
	$(".tabtitle .hotserchbtn").on("click",function(){
		$(".tabcons").eq($(this).index()).css("height","auto").siblings(".tabcons").css("height","0");
	})

	//商户列表分类筛选
    $('.fenlbtn').on('click', function () {
        weui.picker([{
            label: '飞机票',
            value: 0
        }, {
            label: '火车票',
            value: 1
        }, {
            label: '的士票',
            value: 2
        },{
            label: '公交票 (disabled)',
            disabled: true,
            value: 3
        }, {
            label: '其他',
            value: 4
        }], {
            onChange: function (result) {
                console.log(result);
            },
            onConfirm: function (result) {
                console.log(result);
            }
        });
	});

    $('.showdata').on('click', function () {
        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            onChange: function (result) {
                console.log(result);
            },
            onConfirm: function (result) {
                console.log(result);
            }
        });
    });

	//地址选择器
	// $(".home-city").cityPicker({
	// 	title: "请选择收货地址"
	//   });
});

/**
 * 微信ui上传
 * @param option
 * {
		name:'images[]',       图片上传名称
		imagesNumber:4,        最多可上传数量
		changeImagesNumber:4,   还可以上传数量
		inputId:'uploaderInput', 上传file的id名称
		filesId:'uploaderFiles', 存图片的ui标签id名称
		gallery:'gallery',       预览标签id名称
		galleryImg:'galleryImg', 删除的id名称
	}
 */
function weUiImg(option) {
	var uploaderInput = "uploaderInput",
        uploaderFiles = "uploaderFiles",
        gallery = "gallery",
        galleryImg = "galleryImg",
        videoBox = "videobox";
    if (option.hasOwnProperty("inputId")){
        uploaderInput = option.inputId;
	}
    if (option.hasOwnProperty("filesId")){
        uploaderFiles = option.filesId;
	}
    if (option.hasOwnProperty("gallery")){
        gallery = option.gallery;
	}
    if (option.hasOwnProperty("galleryImg")){
        galleryImg = option.galleryImg;
	}
    if (option.hasOwnProperty("videoBox")){
        videoBox = option.videoBox;
	}
    var tmpl = '<li class="weui-uploader__file" style="background-image:url(#url#)"><input type="hidden" value="" name="'+option.name+'" class="_imgUrl_"></li>',
        $gallery = $("#"+gallery), $galleryImg = $("#"+galleryImg),
        $uploaderInput = $("#"+uploaderInput),
        $uploaderFiles = $("#"+uploaderFiles),
		$videoInput = $("."+videoBox),
        reader, imagesNumber = option.imagesNumber, changeImagesNumber = option.changeImagesNumber,
        index //第几张图片
    ;
    $uploaderInput.on("change", function (e) {
    	if ($videoInput.length > 0) {
            $videoInput.hide();
        }
        var src, url = window.URL || window.webkitURL || window.mozURL, files = e.target.files;
        var l = files.length;
        if (l >= changeImagesNumber) {
            l = changeImagesNumber;
        }
        for (var i = 0, len = l; i < len; ++i) {
            var file = files[i];

            if (url) {
                src = url.createObjectURL(file);
            } else {
                src = e.target.result;
            }
            $uploaderFiles.append($(tmpl.replace('#url#', src)));
            reader = new FileReader(file);
            reader.onload = function () {
                $uploaderFiles.find("._imgUrl_").eq(0).val(this.result);
                $uploaderFiles.find("._imgUrl_").eq(0).removeClass('_imgUrl_');
            };
            reader.readAsDataURL(file);
            changeImagesNumber = calculationNumber(imagesNumber, changeImagesNumber, $uploaderFiles, $uploaderInput);
        }
    });
    $uploaderFiles.on("click", "li", function () {
        index = $(this).index();
        $galleryImg.attr("style", this.getAttribute("style"));
        $gallery.fadeIn(100);
    });
    $gallery.on("click", function () {
        $gallery.fadeOut(100);
    });

    //删除图片
    $gallery.find('.weui-gallery__del').on('click', function () {
        // console.log('删除');
        $uploaderFiles.find("li").eq(index).remove();
        changeImagesNumber = calculationNumber(imagesNumber, changeImagesNumber, $uploaderFiles, $uploaderInput);
    });
}

/**
 * 计算图片个数
 */
function calculationNumber(n, c, filesId, inputId) {
    var l = filesId.find('.weui-uploader__file').length;
    if(l >= n){
        inputId.parent('.weui-uploader__input-box').hide();
    }else{
        inputId.parent('.weui-uploader__input-box').show();
    }
    if ($(".weui-uploader_number").length > 0){
        $(".weui-uploader_number").html(l);
	}
    c = n - l;
    return c;
}

$(function () {
	var delParent;
	var defaults = {
		fileType         : ["jpg","png","bmp","jpeg"],   // 上传文件的类型
		fileSize         : 1024 * 1024 * 10                  // 上传文件的大小 10M
	};
	/*点击图片的文本框*/
	$(".image").change(function(){
		var idFile = $(this).attr("id");
		var file = document.getElementById(idFile);
		var imgContainer = $(this).parents(".z_photo"); //存放图片的父亲元素
		var fileList = file.files; //获取的图片文件
		var input = $(this).parent();//文本框的父亲元素
		var imgArr = [];
		//遍历得到的图片文件
		var numUp = imgContainer.find(".up-section").length;
		var totalNum = numUp + fileList.length;  //总的数量
		if(fileList.length > 3 || totalNum > 3 ){
			alert("上传图片数目不可以超过3个，请重新选择");
		}
		else if(numUp < 3){
			fileList = validateUp(fileList);
			for(var i = 0;i<fileList.length;i++){
				var imgUrl = window.URL.createObjectURL(fileList[i]);
				imgArr.push(imgUrl);
				var $section = $("<section class='up-section fl loading'>");
				imgContainer.prepend($section);
				var $span = $("<span class='up-span'>");
				$span.appendTo($section);

				var $img0 = $("<img class='close-upimg'>").on("click",function(event){
					event.preventDefault();
					event.stopPropagation();
					$(".works-mask").show();
					delParent = $(this).parent();
				});
				$img0.attr("src","img/png/pic37.png").appendTo($section);
				var $img = $("<img class='up-img up-opcity'>");
				$img.attr("src",imgArr[i]);
				$img.appendTo($section);
				var $p = $("<p class='img-name-p'>");
				$p.html(fileList[i].name).appendTo($section);
				var $input = $("<input id='taglocation' name='taglocation' value='' type='hidden'>");
				$input.appendTo($section);
				var $input2 = $("<input id='tags' name='tags' value='' type='hidden'/>");
				$input2.appendTo($section);

			}
		}
		setTimeout(function(){
			$(".up-section").removeClass("loading");
			$(".up-img").removeClass("up-opcity");
		},3000);
		numUp = imgContainer.find(".up-section").length;
		if(numUp >= 3){
			$(this).parent().hide();
		}
	});
	$(".z_photo").delegate(".close-upimg","click",function(){
		$(".works-mask").show();
		delParent = $(this).parent();
	});
	$(".wsdel-ok").click(function(){
		$(".works-mask").hide();
		var numUp = delParent.siblings().length;
		if(numUp < 6){
			delParent.parent().find(".z_file").show();
		}
		delParent.remove();
	});
	$(".wsdel-no").click(function(){
		$(".works-mask").hide();
	});

	function validateUp(files){
		var arrFiles = [];//替换的文件数组
		for(var i = 0, file; file = files[i]; i++){
			//获取文件上传的后缀名
			var newStr = file.name.split("").reverse().join("");
			if(newStr.split(".")[0] != null){
				var type = newStr.split(".")[0].split("").reverse().join("");
				console.log(type+"===type===");
				if(jQuery.inArray(type, defaults.fileType) > -1){
					// 类型符合，可以上传
					if (file.size >= defaults.fileSize) {
						alert(file.size);
						alert('您这个"'+ file.name +'"文件大小过大');
					} else {
						// 在这里需要判断当前所有文件中
						arrFiles.push(file);
					}
				}else{
					alert('您这个"'+ file.name +'"上传类型不符合');
				}
			}else{
				alert('您这个"'+ file.name +'"没有类型, 无法识别');
			}
		}
		return arrFiles;
	}
});


//weui
// ios
$(function(){
	$('#showPicker').on('click', function () {
		weui.picker([{
			label: '飞机票',
			value: 0
		}, {
			label: '火车票',
			value: 1
		}, {
			label: '的士票',
			value: 2
		},{
			label: '公交票 (disabled)',
			disabled: true,
			value: 3
		}, {
			label: '其他',
			value: 4
		}], {
			onChange: function (result) {
				console.log(result);
			},
			onConfirm: function (result) {
				console.log(result);
			}
		});
	});

});

$(function(){
	var $iosActionsheet = $('#iosActionsheet');
	var $iosMask = $('#iosMask');

	function hideActionSheet() {
		$iosActionsheet.removeClass('weui-actionsheet_toggle');
		$iosMask.fadeOut(200);
	}

	$iosMask.on('click', hideActionSheet);
	$('#iosActionsheetCancel').on('click', hideActionSheet);
	$("#showIOSActionSheet").on("click", function(){
		$iosActionsheet.addClass('weui-actionsheet_toggle');
		$iosMask.fadeIn(200);
	});
});
$(function(){
	var imgsurl=[];
	var nowurl='';
	var imgObj=$(".imgboxpull img");
	for(var i=0;i<imgObj.length;i++){
		imgsurl[i]=imgObj[i].src;
		imgObj[i].onclick=function(){
			nowurl=this.src;
			wx.previewImage({
				current: nowurl,
				urls: imgsurl
			});
		}
	}
});

$(function(){
	var $searchBar = $('#searchBar'),
		$searchResult = $('#searchResult'),
		$searchText = $('#searchText'),
		$searchInput = $('#searchInput'),
		$searchClear = $('#searchClear'),
		$searchCancel = $('#searchCancel');

	function hideSearchResult(){
		$searchResult.hide();
		$searchInput.val('');
	}
	function cancelSearch(){
		hideSearchResult();
		$searchBar.removeClass('weui-search-bar_focusing');
		$searchText.show();
	}

	$searchText.on('click', function(){
		$searchBar.addClass('weui-search-bar_focusing');
		$searchInput.focus();
	});
	$searchInput
		.on('blur', function () {
			if(!this.value.length) cancelSearch();
		})
		.on('input', function(){
			if(this.value.length) {
				$searchResult.show();
			} else {
				$searchResult.hide();
			}
		})
	;
	$searchClear.on('click', function(){
		hideSearchResult();
		$searchInput.focus();
	});
	$searchCancel.on('click', function(){
		cancelSearch();
		$searchInput.blur();
	});
});
